package com.first.teacher.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.first.common.pojo.EduCourse;
import com.first.common.pojo.EduTeacher;
import com.first.common.pojo.TbShoucangCourse;
import com.first.common.pojo.TbShoucangTeacher;
import com.first.teacher.mapper.EduCourseMapper;
import com.first.teacher.mapper.ShouCangCourseMapper;
import com.first.teacher.mapper.ShouCangTeacherMapper;
import com.first.teacher.service.ShouCangService;
import com.first.teacher.vo.GuanZhuVo;
import com.first.teacher.vo.ShouCangVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * @program: finrsteducation-parent
 * @description:
 * @author: 张玉森
 * @create: 2023-06-05 20:09
 **/
@Controller
public class ShouCangServiceImpl implements ShouCangService {
    @Autowired
    private ShouCangCourseMapper shouCangCourseMapper;
    @Autowired
    private ShouCangTeacherMapper shouCangTeacherMapper;
    @Autowired
    private EduCourseMapper eduCourseMapper;


    @Override
    public Long selectFavoriteCourse(ShouCangVo shouCangVo) {
        QueryWrapper<TbShoucangCourse> wrapper = new QueryWrapper<>();
        wrapper.eq("member_id", shouCangVo.getMemberid());
        wrapper.eq("course_id", shouCangVo.getCourseid());
        return shouCangCourseMapper.selectCount(wrapper);
    }

    @Override
    public Integer deleteFavoriteCourse(ShouCangVo shouCangVo) {
        QueryWrapper<TbShoucangCourse> wrapper = new QueryWrapper<>();
        wrapper.eq("member_id", shouCangVo.getMemberid());
        wrapper.eq("course_id", shouCangVo.getCourseid());
        shouCangCourseMapper.delete(wrapper);
        EduCourse eduCourse = eduCourseMapper.selectById(shouCangVo.getCourseid());
        eduCourse.setViewCount(eduCourse.getViewCount() - 1);
        return eduCourseMapper.updateById(eduCourse);
    }

    @Override
    public Integer addFavoritescourse(ShouCangVo shouCangVo) {
        TbShoucangCourse tbShoucangCourse = new TbShoucangCourse();
        tbShoucangCourse.setCourseId(shouCangVo.getCourseid());
        tbShoucangCourse.setMemberId(shouCangVo.getMemberid());
        tbShoucangCourse.setId(RandomUtil.randomNumbers(19));

        int insert = shouCangCourseMapper.insert(tbShoucangCourse);

        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        wrapper.eq("id", shouCangVo.getCourseid());
        EduCourse eduCourse = eduCourseMapper.selectOne(wrapper);
        eduCourse.setViewCount(eduCourse.getViewCount() + 1);
        return eduCourseMapper.updateById(eduCourse);
    }

    @Override
    public Long findAttentionTeacher(GuanZhuVo guanZhuVo) {
        QueryWrapper<TbShoucangTeacher> wrapper = new QueryWrapper<>();
        wrapper.eq("member_id", guanZhuVo.getMemberid());
        wrapper.eq("teacher_id", guanZhuVo.getTeacherid());
        return shouCangTeacherMapper.selectCount(wrapper);
    }


    @Override
    public Integer deleteAttentionTeacher(GuanZhuVo guanZhuVo) {
        QueryWrapper<TbShoucangTeacher> wrapper = new QueryWrapper<>();
        wrapper.eq("member_id", guanZhuVo.getMemberid());
        wrapper.eq("teacher_id", guanZhuVo.getTeacherid());
        return shouCangTeacherMapper.delete(wrapper);
    }

    @Override
    public Integer addAttentionTeacher(GuanZhuVo guanZhuVo) {
        TbShoucangTeacher tbShoucangTeacher = new TbShoucangTeacher();
        tbShoucangTeacher.setTeacherId(guanZhuVo.getTeacherid());
        tbShoucangTeacher.setMemberId(guanZhuVo.getMemberid());
        tbShoucangTeacher.setId(RandomUtil.randomNumbers(19));
        return shouCangTeacherMapper.insert(tbShoucangTeacher);
    }

    @Override
    public EduTeacher findTeacherBYMobile(String mobile) {
        return shouCangTeacherMapper.findTeacherByMobile(mobile);
    }

    /**
     * @param id
     * @return 我的收藏
     */
    @Override
    public List<EduCourse> findFavoriteCourseById(String id) {
        List<EduCourse> favoriteCourseById = shouCangCourseMapper.findFavoriteCourseById(id);
        return favoriteCourseById;
    }

    /**
     * @param id
     * @return 我的关注
     */
    @Override
    public List<EduTeacher> findAttentionTeacher2(String id) {
        List<EduTeacher> attentionTeacher = shouCangTeacherMapper.findAttentionTeacher(id);
        return attentionTeacher;
    }


}
